package com.shenma2009.ex;

import com.shenma2009.domain.ResultResponse;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.IOException;

/**
 * @author 军哥
 * @version 1.0
 * @description: GlobalExceptionHandler
 * @date 2023/4/21 10:55
 */

@ControllerAdvice
public class GlobalExceptionHandler {
    //空指针异常
    @ExceptionHandler(value = NullPointerException.class)
    @ResponseBody
    public ResultResponse nullPointerExceptionHandler(NullPointerException ex) {
        ex.printStackTrace();
        return ResultResponse.FAILED(500, "服务器发生未知错误");
    }

    //类型转换异常
    @ExceptionHandler(ClassCastException.class)
    @ResponseBody
    public ResultResponse classCastExceptionHandler(ClassCastException ex) {
        ex.printStackTrace();
        return ResultResponse.FAILED(501, "服务器发生未知错误");
    }

    //IO异常
    @ExceptionHandler(IOException.class)
    @ResponseBody
    public ResultResponse iOExceptionHandler(IOException ex) {
        ex.printStackTrace();
        return ResultResponse.FAILED(502, "服务器发生未知错误");
    }

    //IO异常
    @ExceptionHandler(Exception.class)
    @ResponseBody
    public ResultResponse xceptionHandler(IOException ex) {
        ex.printStackTrace();
        return ResultResponse.FAILED(503, "服务器发生未知错误");
    }
}
